package com.sznaner.tool.util;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.view.WindowManager;

import java.lang.reflect.Method;

public class SystemUtil {
    public static Boolean isAndroid_8(){
        int version = Build.VERSION.SDK_INT;
        if(version >=26) return true;
        return false;
    }
    public static Boolean isAndroid_7(){
        int version = Build.VERSION.SDK_INT;
        if(version >=24) return true;
        return false;
    }
    public static int getScreenWidth() {
        WindowManager wm = (WindowManager) NRTool.context()
                .getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }

    public static int getScreenHeight( ) {
        WindowManager wm = (WindowManager)  NRTool.context()
                .getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getHeight();
    }

    //获取设备名称
    public static String  getDeviceName(){
        String name = "";
        if(Build.MODEL != null){
            name = Build.MODEL;
        }
        return name;
    }

    //获取SN号
    public static String getSN(){
        String serialNumber = android.os.Build.SERIAL;
        return serialNumber;
    }

    //获取YHK build.yhk.id
    public static String getYhkBuildID(){
        return systemProperties_get("ro.build.yhk.id","unknown");
    }
    public static String getAndroidVersion(){
        return android.os.Build.VERSION.RELEASE;
    }

    //获取ID号
    //<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
    public static String getDeviceID( ) {
        String id;
        TelephonyManager mTelephony = (TelephonyManager)  NRTool.context().getSystemService(Context.TELEPHONY_SERVICE);
        if (mTelephony.getDeviceId() != null) {
            id = mTelephony.getDeviceId();
        } else {
            id = Settings.Secure.getString( NRTool.context().getContentResolver(), Settings.Secure.ANDROID_ID);
        }
        return id;
    }



    //反射实现系统隐藏方法:SystemProperties.get()
    public static  String systemProperties_get(String key, String defaultValue) {
        String value = defaultValue;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class, String.class);
            value = (String)(get.invoke(c, key, "unknown" ));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return value;
        }
    }

    /**
     * 恢复出厂设置，需要系统权限，以及系统签名(android8.1有效,其他版本未测试)
     */
    public static void resetSystem( ) {

        Intent intent = new Intent("android.intent.action.FACTORY_RESET");
        intent.setPackage("android");
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        intent.putExtra("android.intent.extra.REASON", "doFactoryReset");
        intent.putExtra("android.intent.extra.WIPE_EXTERNAL_STORAGE", false);
        intent.putExtra("android.intent.extra.EXTRA_WIPE_ESIMS", true);
        NRTool.context().sendBroadcast(intent);
    }

}
